<?php    
/**
 * Custom controller for news feeds.
 */
class ControllerCatalogNewsFeed extends Controller { 
	private $error = array();
  
	/**
	 * For indexing operations.
	 */
  	public function index() {
		// Load languages
		$this->load->language('catalog/newsfeed');
		
		$this->document->title = $this->language->get('heading_title');
		 
		// Load the model
		$this->load->model('catalog/newsfeed');
		
    	$this->getList();
  	}
  
	/**
	 * For news feed insert operations.
	 */
  	public function insert() {
		// Load language
		$this->load->language('catalog/newsfeed');

    	$this->document->title = $this->language->get('heading_title');
		
		// Load model
		$this->load->model('catalog/newsfeed');
			
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_catalog_newsfeed->addNews($this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';
			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=catalog/newsfeed' . $url);
		}
    
    	$this->getForm();
  	} 
   
	/**
	 * For news feed update operations.
	 */
  	public function update() {
		$this->load->language('catalog/newsfeed');

    	$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('catalog/newsfeed');
		
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_catalog_newsfeed->editNews($this->request->get['news_id'], $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=catalog/newsfeed' . $url);
		}
    
    	$this->getForm();
  	}   

	/**
	 * For news feed delete operations.
	 */
  	public function delete() {
		$this->load->language('catalog/newsfeed');

    	$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('catalog/newsfeed');
			
    	if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $news_id) {
				$this->model_catalog_newsfeed->deleteNews($news_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';
			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=catalog/newsfeed' . $url);
    	}
	
    	$this->getList();
  	}  
    
  	private function getList() {
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'name';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		$url = '';
			
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

  		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home',
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=catalog/newsfeed' . $url,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
							
		$this->data['insert'] = HTTPS_SERVER . 'index.php?route=catalog/newsfeed/insert' . $url;
		$this->data['delete'] = HTTPS_SERVER . 'index.php?route=catalog/newsfeed/delete' . $url;	

		$this->data['news'] = array();

		$data = array(
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		
		// Get the total number of news feeds available
		$news_total = $this->model_catalog_newsfeed->getTotalNews();
	
		$results = $this->model_catalog_newsfeed->getAllNews($data);
 
    	foreach ($results as $result) {
			$action = array();
			
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => HTTPS_SERVER . 'index.php?route=catalog/newsfeed/update&news_id=' . $result['news_id'] . $url
			);
						
			$this->data['news'][] = array(
				'news_id' 	 => $result['news_id'],
				'news_item'  => $result['news_item'],
				'author'     => $result['author'],
				'published'	 => $result['is_published'],
				'selected'   => isset($this->request->post['selected']) && in_array($result['news_id'], $this->request->post['selected']),
				'action'     => $action
			);
		}	
	
		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		
		$this->data['text_published'] = $this->language->get('text_published');
		$this->data['text_not_published'] = $this->language->get('text_not_published');
		$this->data['text_characters_left'] = $this->language->get('text_characters_left');
		
		// Column labels
		$this->data['column_news_item'] = $this->language->get('column_news_item');
		$this->data['column_author'] = $this->language->get('column_author');
		$this->data['column_published'] = $this->language->get('column_published');			
		$this->data['column_action'] = $this->language->get('column_action');			
		
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=' .  'DESC';
		} else {
			$url .= '&order=' .  'ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		$this->data['sort_name'] = HTTPS_SERVER . 'index.php?route=catalog/newsfeed&sort=name' . $url;
		$this->data['sort_sort_order'] = HTTPS_SERVER . 'index.php?route=catalog/newsfeed&sort=sort_order' . $url;
		
		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $news_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = HTTPS_SERVER . 'index.php?route=catalog/newsfeed' . $url . '&page={page}';
			
		$this->data['pagination'] = $pagination->render();

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;
		
		$this->template = 'catalog/newsfeed_list.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
  
	/**
	 * Create form for inputs.
	 */
  	private function getForm() {
    	$this->data['heading_title'] = $this->language->get('heading_title');

    	$this->data['text_enabled'] = $this->language->get('text_enabled');
    	$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_default'] = $this->language->get('text_default');
    	$this->data['text_published'] = $this->language->get('text_published');
		$this->data['text_not_published'] = $this->language->get('text_not_published');
		$this->data['text_characters_left'] = $this->language->get('text_characters_left');
		
		$this->data['entry_author'] = $this->language->get('entry_author');
		$this->data['entry_published'] = $this->language->get('entry_published');
		$this->data['entry_news_item'] = $this->language->get('entry_news_item');
	
  
    	$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');
	  
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

 		if (isset($this->error['news_item'])) {
			$this->data['error_news_item'] = $this->error['news_item'];
		} else {
			$this->data['error_news_item'] = '';
		}

  		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home',
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=catalog/newsfeed',
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
		    
		$url = '';
			
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
							
		if (!isset($this->request->get['news_id'])) {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=catalog/newsfeed/insert' . $url;
		} else {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=catalog/newsfeed/update&news_id=' . $this->request->get['news_id'] . $url;
		}
		
		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=catalog/newsfeed' . $url;

		// Edits
    	if (isset($this->request->get['news_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$news_info = $this->model_catalog_newsfeed->getNews($this->request->get['news_id']);
			$news_item_info = $this->model_catalog_newsfeed->getNewsItem($this->request->get['news_id']);
    	}
					
		
		// News author
    	if (isset($this->request->post['author'])) {
      		$this->data['author'] = $this->request->post['author'];
    	} elseif (isset($news_info)) {
			$this->data['author'] = $news_info['author'];
		} else {	
      		$this->data['author'] = '';
    	}
		
		// Language information
		$this->load->model('localisation/language');
		
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
		
		// News item
		if (isset($this->request->post['news_item'])) {
			$this->data['news_item'] = $this->request->post['news_item'];
		} elseif (isset($news_item_info)) {
			$this->data['news_item'] = $news_item_info;
		} else {
			$this->data['news_item'] = array();
		}
		
		// Published
		if (isset($this->request->post['published'])) {
			$this->data['published'] = $this->request->post['is_published'];
		} elseif (isset($news_info)) {
			$this->data['published'] = $news_info['is_published'];
		} else {
			$this->data['published'] = 0;
		}
				
		$this->template = 'catalog/newsfeed_form.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}  
	 
	 /**
	  * Performs validation on form data
	  */ 
  	private function validateForm() {
    	if (!$this->user->hasPermission('modify', 'catalog/newsfeed')) {
      		$this->error['warning'] = $this->language->get('error_permission');
    	}

		// For each language
		foreach ($this->request->post['news_item'] as $language_id => $value) {
			if ((strlen(utf8_decode($value['news_item'])) < 3) || (strlen(utf8_decode($value['news_item'])) > 255)) {
				$this->error['news_item'][$language_id] = $this->language->get('error_news_item');
			}
		}
		
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
  	}    

	/**
	 * Validate if user can delete news/news items.
	 */
  	private function validateDelete() {
    	if (!$this->user->hasPermission('modify', 'catalog/newsfeed')) {
			$this->error['warning'] = $this->language->get('error_permission');
    	}	
	
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}  
  	}
}
?>